package com.pet.server.ext.action.mall.product;

import java.util.List;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.Result;
import com.pet.server.ext.data.ConstantUi;
import com.pet.server.ext.data.Page;
import com.pet.server.ext.data.ProductInfo;
import com.pet.server.ext.service.TbkService;
import com.pet.server.ext.util.JsonUtil;
import com.pet.server.ext.util.NumberUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;
import com.pet.server.ext.util.cache.ConstantUiUtil;

/**
 * 搜索
 * */
public class Search extends ActionBase{

	private String[] keywords;//关键字
	private String pet_code,cate_code,ju_code,sort;
	private Double min_price,max_price;
	private Page page;
	
	public Search(){
		 needCheckAuth =false;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {	
		String keyword=params.getFieldValue("keyword");
		
		if(!StringUtil.isEmpty(keyword)){
			keywords=keyword.split("\\s");
		}
		
		ju_code=params.getFieldValue("ju_code");//聚合code
		pet_code=params.getFieldValue("pet_code");
		cate_code=params.getFieldValue("cate_code");
		min_price=NumberUtil.getDouble(params.getFieldValue("min_price"));
		max_price=NumberUtil.getDouble(params.getFieldValue("max_price"));
		sort=params.getFieldValue("sort");
		page=super.newPage(params);
		
	}
	
	@Override
	public Result invokeService() throws ApiError {
		
		
		System.out.println(pet_code+"==========================");
		
		//优先聚合code
		if(!StringUtil.isEmpty(ju_code)){
			ConstantUi con = ConstantUiUtil.getInstance().findConstant(ju_code);
			if(null!=con){
				if(!StringUtil.isEmpty(con.pet_breeds)){
					pet_code=con.pet_breeds;
				}
				if(!StringUtil.isEmpty(con.pet_cate)){
					cate_code=con.pet_cate;
				}
			}
		}
		
		TbkService tbkService = new TbkService();
		tbkService.searchTbkProductInfo(page, pet_code, cate_code, min_price, max_price, keywords,sort);
		
		List<ProductInfo> list = (List<ProductInfo>)page.getList();
		Result result = new Result();
		result.setPage(page, JsonUtil.parseBaseJsonArray(list));
		
		//result.put("data",JsonUtil.parseBaseJsonArray(list));
		return result;
	}
}
